/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CONTROLEUR;

import CONTROLEUR.exceptions.NonexistentEntityException;
import CONTROLEUR.exceptions.PreexistingEntityException;
import MODEL.Acheter;
import MODEL.AcheterPK;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import MODEL.Client;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author ahmed
 */
public class AcheterJpaController implements Serializable {

    public AcheterJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Acheter acheter) throws PreexistingEntityException, Exception {
        if (acheter.getAcheterPK() == null) {
            acheter.setAcheterPK(new AcheterPK());
        }
        acheter.getAcheterPK().setIdC(acheter.getClient().getIdC());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Client client = acheter.getClient();
            if (client != null) {
                client = em.getReference(client.getClass(), client.getIdC());
                acheter.setClient(client);
            }
            em.persist(acheter);
            if (client != null) {
                client.getAcheterCollection().add(acheter);
                client = em.merge(client);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findAcheter(acheter.getAcheterPK()) != null) {
                throw new PreexistingEntityException("Acheter " + acheter + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Acheter acheter) throws NonexistentEntityException, Exception {
        acheter.getAcheterPK().setIdC(acheter.getClient().getIdC());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Acheter persistentAcheter = em.find(Acheter.class, acheter.getAcheterPK());
            Client clientOld = persistentAcheter.getClient();
            Client clientNew = acheter.getClient();
            if (clientNew != null) {
                clientNew = em.getReference(clientNew.getClass(), clientNew.getIdC());
                acheter.setClient(clientNew);
            }
            acheter = em.merge(acheter);
            if (clientOld != null && !clientOld.equals(clientNew)) {
                clientOld.getAcheterCollection().remove(acheter);
                clientOld = em.merge(clientOld);
            }
            if (clientNew != null && !clientNew.equals(clientOld)) {
                clientNew.getAcheterCollection().add(acheter);
                clientNew = em.merge(clientNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                AcheterPK id = acheter.getAcheterPK();
                if (findAcheter(id) == null) {
                    throw new NonexistentEntityException("The acheter with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(AcheterPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Acheter acheter;
            try {
                acheter = em.getReference(Acheter.class, id);
                acheter.getAcheterPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The acheter with id " + id + " no longer exists.", enfe);
            }
            Client client = acheter.getClient();
            if (client != null) {
                client.getAcheterCollection().remove(acheter);
                client = em.merge(client);
            }
            em.remove(acheter);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Acheter> findAcheterEntities() {
        return findAcheterEntities(true, -1, -1);
    }

    public List<Acheter> findAcheterEntities(int maxResults, int firstResult) {
        return findAcheterEntities(false, maxResults, firstResult);
    }

    private List<Acheter> findAcheterEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Acheter.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Acheter findAcheter(AcheterPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Acheter.class, id);
        } finally {
            em.close();
        }
    }

    public int getAcheterCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Acheter> rt = cq.from(Acheter.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
